import hashlib
import json
import os.path
from urllib.request import urlretrieve

import requests
from bs4 import BeautifulSoup

from utils import Download

with open('./esp6500.json', 'r') as f:
    data_source = json.load(f)
baseurl = data_source['baseurl']

response = requests.get(url=baseurl)
if response.status_code != 200:
    raise ValueError

soup = BeautifulSoup(response.text, 'html.parser')
tabs_7_a = soup.select_one('div#tabs-7').find_all('a')

url = tabs_7_a[1]['href']
filename = tabs_7_a[1].text
path = os.path.join('download', 'esp6500', filename)

# 下载文件
download = Download(url, fileName=filename, dirName='esp6500')
md5 = download.start()

data_source['path'] = path
data_source['md5'] = md5

with open('esp6500.json', 'w') as f:
    json.dump(data_source, f)
